package com.ruoyi.gis.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gis.domain.GisDictData;
import com.ruoyi.gis.domain.GisDictType;
import com.ruoyi.gis.mapper.GisDictDataMapper;
import com.ruoyi.gis.mapper.GisDictTypeMapper;
import com.ruoyi.gis.service.IGisDictTypeService;
import com.ruoyi.gis.utils.GisDictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文件字典类型Service业务层处理
 *
 * @author rww
 * @date 2023-02-20
 */
@Service
public class GisDictTypeServiceImpl implements IGisDictTypeService
{
    @Autowired
    private GisDictTypeMapper gisDictTypeMapper;
    @Autowired
    private GisDictDataMapper gisDictDataMapper;

    /**
     * 查询文件字典类型
     *
     * @param dictId 文件字典类型主键
     * @return 文件字典类型
     */
    @Override
    public GisDictType selectGisDictTypeByDictId(Long dictId)
    {
        return gisDictTypeMapper.selectGisDictTypeByDictId(dictId);
    }

    /**
     * 查询文件字典类型列表
     *
     * @param GisDictType 文件字典类型
     * @return 文件字典类型
     */
    @Override
    public List<GisDictType> selectGisDictTypeList(GisDictType GisDictType)
    {
        return gisDictTypeMapper.selectGisDictTypeList(GisDictType);
    }

    /**
     * 新增文件字典类型
     *
     * @param GisDictType 文件字典类型
     * @return 结果
     */
    @Override
    public int insertGisDictType(GisDictType GisDictType)
    {
        GisDictType.setCreateTime(DateUtils.getNowDate());
        return gisDictTypeMapper.insertGisDictType(GisDictType);
    }


    @Override
    public String checkDictTypeUnique(GisDictType dict)
    {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        GisDictType dictType = gisDictTypeMapper.checkDictTypeUnique(dict.getDictType());
        if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    @Override
    @Transactional
    public int updateDictType(GisDictType dict)
    {
        GisDictType oldDict = gisDictTypeMapper.selectGisDictTypeByDictId(dict.getDictId());
        gisDictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        int row = gisDictTypeMapper.updateGisDictType(dict);
        if (row > 0)
        {
            List<GisDictData> dictDatas = gisDictDataMapper.selectDictDataByType(dict.getDictType());
            GisDictUtils.setDictCache(dict.getDictType(), dictDatas);
        }
        return row;
    }


    /**
     * 修改文件字典类型
     *
     * @param GisDictType 文件字典类型
     * @return 结果
     */
    @Override
    public int updateGisDictType(GisDictType GisDictType)
    {
        GisDictType.setUpdateTime(DateUtils.getNowDate());
        return gisDictTypeMapper.updateGisDictType(GisDictType);
    }

    /**
     * 批量删除文件字典类型
     *
     * @param dictIds 需要删除的文件字典类型主键
     * @return 结果
     */
    @Override
    public int deleteGisDictTypeByDictIds(Long[] dictIds)
    {
        return gisDictTypeMapper.deleteGisDictTypeByDictIds(dictIds);
    }

    /**
     * 删除文件字典类型信息
     *
     * @param dictId 文件字典类型主键
     * @return 结果
     */
    @Override
    public int deleteGisDictTypeByDictId(Long dictId)
    {
        return gisDictTypeMapper.deleteGisDictTypeByDictId(dictId);
    }


    @Override
    public void resetDictCache()
    {
        clearDictCache();
        loadingDictCache();
    }

    @Override
    public void clearDictCache()
    {
        GisDictUtils.clearDictCache();
    }
    @Override
    public void loadingDictCache()
    {
        GisDictData dictData = new GisDictData();
        dictData.setStatus("0");
        Map<String, List<GisDictData>> dictDataMap = gisDictDataMapper.selectGisDictDataList(dictData).stream().collect(Collectors.groupingBy(GisDictData::getDictType));
        for (Map.Entry<String, List<GisDictData>> entry : dictDataMap.entrySet())
        {
            GisDictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(GisDictData::getDictSort)).collect(Collectors.toList()));
        }
    }

    @Override
    public List<GisDictType> selectDictTypeAll()
    {
        return gisDictTypeMapper.selectDictTypeAll();
    }


    @Override
    public List<GisDictData> selectDictDataByType(String dictType)
    {
        List<GisDictData> dictDatas = GisDictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = gisDictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            GisDictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }
}
